//
//  DHSearchSceneCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/2/10.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSearchSceneCell.h"
#import "DHSearchSceneItemView.h"

@interface DHSearchSceneCell()

@property (strong, nonatomic) UIScrollView *scrollView;

@property (strong, nonatomic) NSArray *dataArr;

@end

@implementation DHSearchSceneCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        if (!self.scrollView) {
            self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 54)];
            [self.contentView addSubview:self.scrollView];
        }
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 53, kScreenWidth, 1)];
        line.backgroundColor = [UIColor colorWithHexString:@"#ECF2F1"];
        [self.contentView addSubview:line];
    }
    return self;
}

- (NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSArray array];
    }
    return _dataArr;
}

- (void)layoutSubviews{
    self.scrollView.frame = CGRectMake(0, 0, self.width, self.height);
}

- (void)updateCellWithConditionArr:(NSArray *)conditionArr firstLoad:(BOOL)firstLoad{
    self.dataArr = conditionArr;
    if (firstLoad) {
        [self.scrollView removeAllSubviews];
        self.dataArr = conditionArr;
        __block CGFloat spaceX = 5;
        __block CGFloat itemLeft = spaceX;
        __weak __typeof(self)weakSelf = self;
        [conditionArr enumerateObjectsUsingBlock:^(DHScenarioModel *scene, NSUInteger idx, BOOL * _Nonnull stop) {
            CGFloat itemW = 30 + [scene.title widthForFont:[UIFont systemFontOfSize:14]];
            DHSearchSceneItemView *sceneItemView = [[DHSearchSceneItemView alloc] initWithFrame:CGRectMake(itemLeft, 0, itemW, weakSelf.height) andSceneTitle:scene.title];
            [weakSelf.scrollView addSubview:sceneItemView];
            [sceneItemView.sceneButton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
            sceneItemView.sceneButton.tag = idx;
            itemLeft += (itemW + spaceX);
        }];
        self.scrollView.contentSize = CGSizeMake(itemLeft, self.height);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)clickAction:(UIControl *)sender{
    for (DHSearchSceneItemView *item in self.scrollView.subviews) {
        if (item.sceneButton.tag != sender.tag) {
            item.sceneButton.selected = YES;
            [item clickAction:item.sceneButton];
        }
    }
    DHScenarioModel *model = self.dataArr[sender.tag];
    if (sender.isSelected) {
        [self routerEventWithName:@"DHSearchSceneCell" userInfo:@{@"scene_id":model._id}];
    }else{
        [self routerEventWithName:@"DHSearchSceneCell" userInfo:@{@"scene_id":[NSNull null]}];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
